/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Jan-25-2024 11:11:52
*
*/

#include "FreeRTOS.h"


#include "uart_parse.h"
#include "stdio.h"
#include "Caige.h"
#include "input.h"
#include <string.h>
#include "device.h"

#include "key.h"
#include "lcd.h"
#include "flash.h"



KEY_TYPE key_st[KEYn];


void init_key()
{
    uint8_t i;

    //gd_eval_key_init(KEY_WAKEUP, KEY_MODE_EXTI);
    //gd_eval_key_init(KEY_TAMPER, KEY_MODE_EXTI);
    for(i = 0; i < KEYn; i++) {
        gd_eval_key_init(i, KEY_MODE_EXTI);
        key_st[i].down_times = 0;
        key_st[i].up_times = 0;
        key_st[i].count = 0;
        key_st[i].key_init = 1;
        key_st[i].func = 0;
        key_st[i].func_long = 0;
    }

    key_st[0].func = toggle_trip_odo;
    key_st[0].func_long = trip_clean;//
    //key_st[0].func_long = toggle_mph_display;//
    //toggle_mph_display;//toggle_trip_odo;//
    //key_st[1].func = toggle_trip_odo;//guide_test;//
}

void key_down_start(uint8_t index)
{
    if(gpio_input_bit_get(KEY_PORT[index], KEY_PIN[index]) == RESET) {
        key_st[index].down_times = DOWN_CHECK_TIME_10MS;
        key_st[index].up_times = 0;

    } else {
        key_st[index].down_times = 0;
        key_st[index].up_times = UP_CHECK_TIME_10MS;
    }

    key_st[index].key_init = 0;
    nvic_irq_disable(KEY_IRQn[index]);
}

void key_handle(void)
{
    uint8_t i;

    for(i = 0; i < KEYn; i++) {
        if(key_st[i].down_times) {
            key_st[i].down_times--;

            if(gpio_input_bit_get(KEY_PORT[i], KEY_PIN[i])) { //key up
                key_st[i].count = 0;
                key_st[i].down_times = 0;
                key_st[i].up_times = UP_CHECK_TIME_10MS;

            } else { //key_down
                key_st[i].count++;
                //if(key_st[i].down_times==0)
                //key_st[i].down_times = DOWN_CHECK_TIME_10MS;
            }

            if(key_st[i].down_times == 0) {
                if(key_st[i].count >= DOWN_CHECK_TIMES) {
                    //log_debug("%d down\n", key_st[i].count);; //reboot();    //key real down

                    //key_st[i].count = 0;
                    if(gpio_input_bit_get(KEY_PORT[i], KEY_PIN[i])) { //key up
                        //key_st[i].count = 0;
                        //key_st[i].down_times = 0;
                        //key_st[i].up_times = UP_CHECK_TIME_10MS;
                    } else { //key_down
                        //key_st[i].count++;
                        //if(key_st[i].down_times==0)
                        if(key_st[i].count > DOWN_CHECK_TIMES_LONG) {
                            //key_st[i].up_times = UP_CHECK_TIME_10MS;
                            key_st[i].down_times = 0;

                            //key_st[i].count=0;
                            if(key_st[i].func_long) {
                                key_st[i].func_long();
                            }

                            if(key_st[i].key_init == 0) {
                                nvic_irq_enable(KEY_IRQn[i], 2);
                                //log_debug("%d enable\n", i);
                                key_st[i].key_init = 1;
                                //key_st[i].count = 0;
                            }

                        } else {
                            key_st[i].down_times = DOWN_CHECK_TIME_10MS;
                        }
                    }

                } else {
                    //log_debug("%d not down count %d\n", i, key_st[i].count);
                    if(key_st[i].key_init == 0) {
                        nvic_irq_enable(KEY_IRQn[i], 2);
                        //log_debug("%d enable\n", i);
                        key_st[i].key_init = 1;
                        key_st[i].count = 0;
                    }
                }
            }
        }

        if(key_st[i].up_times) {
            key_st[i].up_times--;

            if(key_st[i].key_init == 0) {
                if(gpio_input_bit_get(KEY_PORT[i], KEY_PIN[i])) { //key up
                    key_st[i].count++;

                } else {
                    key_st[i].down_times = DOWN_CHECK_TIME_10MS;
                    key_st[i].count = 0;
                }

                if(key_st[i].count >= DOWN_CHECK_TIMES) {
                    if(key_st[i].count >= DOWN_CHECK_TIMES_LONG) {
                    } else {
                        if(key_st[i].func) {
                            key_st[i].func();
                        }
                    }

                    nvic_irq_enable(KEY_IRQn[i], 2);
                    //log_debug("%d up\n", key_st[i].count);
                    key_st[i].key_init = 1;
                    key_st[i].count = 0;
                }
            }
        }
    }

    /*if(key_st[KEY_WAKEUP].exit_times) {
        if(gpio_input_bit_get(KEY_PORT[KEY_WAKEUP], KEY_PIN[KEY_WAKEUP]) == RESET) { //key down
            key_st[KEY_WAKEUP].exit_times--;

            if(key_st[KEY_WAKEUP].exit_times == 0) {
                ;//reboot();    //key real down
            }

        } else { //key_up
            key_st[KEY_WAKEUP].exit_times = 0;
        }

    } else {
        if(key_st[KEY_WAKEUP].key_init == 0) {
            if(gpio_input_bit_get(KEY_PORT[KEY_WAKEUP], KEY_PIN[KEY_WAKEUP])) { //key up
                gd_eval_key_init(KEY_WAKEUP, KEY_MODE_EXTI);//init key gpio exti
            }
        }
    }
    if(key_st[KEY_TAMPER].exit_times) {
        if(gpio_input_bit_get(KEY_PORT[KEY_TAMPER], KEY_PIN[KEY_TAMPER]) == RESET) { //key down
            key_st[KEY_TAMPER].exit_times--;

            if(key_st[KEY_TAMPER].exit_times == 0) {
                toggle_mph_display();
                update_mph_display();    //key real down
            }

        } else { //key_up
            key_st[KEY_TAMPER].exit_times = 0;
        }

    } else {
        if(key_st[KEY_TAMPER].key_init == 0) {
            if(gpio_input_bit_get(KEY_PORT[KEY_TAMPER], KEY_PIN[KEY_TAMPER])) { //key up
                gd_eval_key_init(KEY_TAMPER, KEY_MODE_EXTI);//init key gpio exti

            }
        }
    }*/
}


